package com.qk.management.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.qk.doman.clue.BusinessCountDO;
import com.qk.doman.clue.ClueCountDO;
import com.qk.entity.OverviewVO;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.ClueMapper;
import com.qk.management.service.ReportOverviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author 16778
 * @date 2025/10/15 14:53
 * @description
 */
@Slf4j
@Service
public class ReportOverviewServiceImpl implements ReportOverviewService {

    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private StringRedisTemplate redisClient;

    private static final String CACHE_PORTAL_KEY_PREFIX = "qk:";
    @Override
    public OverviewVO reportOverview() {
        ClueCountDO clueCountDO = clueMapper.count();
        System.out.println("======================"+clueCountDO);
        BusinessCountDO businessCountDO = businessMapper.count();
        OverviewVO vo = BeanUtil.copyProperties(clueCountDO, OverviewVO.class);
        BeanUtil.copyProperties(businessCountDO, vo);
        return vo;
    }

    @Override
    public OverviewVO reportOverviewWithCache() {
        String redisKey = CACHE_PORTAL_KEY_PREFIX + "reportOverview";
        String overview = null;
        try {
            overview = redisClient.opsForValue().get(redisKey);
            if(!Objects.isNull( overview)){
                return JSONUtil.toBean(overview, OverviewVO.class);
            }
        } catch (Exception e) {
            log.error("redis异常：{}", e.getMessage());
        }
        OverviewVO overviewVO = reportOverview();
        try {
            redisClient.opsForValue().set(redisKey, JSONUtil.toJsonStr(overviewVO));
        } catch (Exception e) {
            log.error("redis异常：{}", e.getMessage());
        }
        return overviewVO;
    }
}
